17. Write Your Own ViewModel Test

Now that you've seen how to write a test, write one on your own!

In this step, using the skills you've learned, you'll practice writing another TasksViewModel test.

Step 1: Write your own ViewModel test

You'll writegetTasksAddViewVisible(). This test should check that if you've set your filter type to show all tasks, that the Add task button is visible.

  1. Using for addNewTask_setsNewTaskEvent() for reference, write a test in TasksViewModelTest called setFilterAllTasks_tasksAddViewVisible() that:
    • Sets the filtering mode to ALL_TASKS
    • Assets that the tasksAddViewVisible LiveData is true

Here's some code you can use to get started:

TasksViewModelTest

    @Test
    fun setFilterAllTasks_tasksAddViewVisible() {

        // Given a fresh ViewModel

        // When the filter type is ALL_TASKS

        // Then the "Add task" action is visible

    }

Note:

  • The TasksFilterType enum for all tasks is ALL_TASKS
  • The visibility of the button to add a task is controlled by the LiveData tasksAddViewVisible.
  1. Once you're done, run your test.